<?php
class AdsController extends AppController
{
	var $name = 'Ads';
	var $helpers = array('Html','Form','Ads.Ads','Cms.Index');
	var $uses = array('Ads.Ad','Ads.Zone');
	
	function beforeFilter()
	{
		if(cr('cmsInstalled'))
		{
			cri('Ads.Zone')->init();
		}
		parent::beforeFilter();
	}
	function admin_index()
	{
		$this->pageTitle = __("Ads");
		$this->paginate['limit'] = 10;
		$this->paginate['order'] = array('created DESC');
		$this->paginate['conditions'] = array('Ad.zone'=>$this->Ad->_getZoneOptionList()) ;
		$adsData = $this->paginate();
		$this->set(compact('adsData'));
	}

	function admin_add()
	{
		$zoneList = $this->Ad->_getZoneOptionList();
		$this->Ad->setFieldProperties('zone',array('options'=>$zoneList));
		if(empty($zoneList))
		{
			$this->warningMsg(__('No zone(s) exists. Please define some ad zones first.'));
			$this->redirect('/admin/ads/editZone');
		}
		$this->pageTitle = __("New Ad");
		if(!empty($this->data))
		{
			$this->Ad->create();
			if($this->Ad->save($this->data))
			{
				$this->successMsg(__('Ad has been saved successfully!'));
				$this->redirect('/admin/ads/index');
			}
			else
			{
				$this->errorMsg(__('Ad has not been saved. Try again!'));
			}
		}else
		{
			$this->Ad->setFieldProperties('published',array('checked'=>true));
		}	
	}

	function admin_edit($id = null)
	{		
		$zoneList = $this->Ad->_getZoneOptionList();
		$this->Ad->setFieldProperties('zone',array('options'=>$zoneList));
		
		$adData = $this->Ad->findById($id);
		$this->pageTitle = sprintf(__("Edit %s"),$adData['Ad']['title']);
				
		if(empty($adData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/ads');
		}
		if(!empty($this->data))
		{
			$this->Ad->id = $id;
			if($this->Ad->save($this->data))
			{
				$this->successMsg(__('Ad has been updated successfully!'));
				$this->redirect('/admin/ads');
			}else
			{
				$this->errorMsg(__('Ad has not been updated. Please try again!'));
			}
		}
		if(empty($this->data))
		{
			$this->data = $adData;
		}
	}

	function admin_view($id = null)
	{
		$this->pageTitle = __("Ads view");
		$adData = $this->Ad->findById($id);
		if(empty($adData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/ads');
		}
		$this->set(compact('adData'));
	}
	
	function admin_delete($id = null)
	{
		$adData = $this->Ad->findById($id);
		if(empty($adData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/ads');
		}
		
		$this->Ad->id =$id ;
		if($this->Ad->delete($id))
		{
			$this->successMsg(__('Ad has been deleted successfully!'));
		}
		else
		{
			$this->errorMsg(__('Unable to delete the specified ad.'));
		}
		$this->redirect('/admin/ads');
	}

	function admin_random_view($zone = null)
	{
		$this->pageTitle = sprintf(__("Random view for zone %s"),Inflector::humanize($zone));
		if(empty($zone))
		{
			$this->errorMsg(__('Wrong parameter!'));
			$this->redirect('/admin/ads/index');
		}
		$this->set(compact('zone'));
	}
	
	function admin_publish($id = null)
	{
		$adData = $this->Ad->findById($id);
		if(empty($adData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/ads');
		}
		$this->Ad->id =$id;
		if($this->Ad->saveField('published',Ad::PUBLISHED))
		{
			$this->successMsg(__('Ad has been published.'));
		}else
		{
			$this->errorMsg(__('Sorry, Ad could not be published.'));
		}
		$this->redirect('/admin/ads/index');

	}

	function admin_unpublish($id = null)
	{
		$adData = $this->Ad->findById($id);
		if(empty($adData))
		{
			$this->errorMsg(__('Invalid URL'));
			$this->redirect('/admin/ads');
		}
		$this->Ad->id =$id;
		if($this->Ad->saveField('published',Ad::UNPUBLISHED))
		{
			$this->successMsg(__('Ad has been unpublished!'));
		}else
		{
			$this->errorMsg(__('Sorry, Ad could not be unpublished.'));
		}
		$this->redirect('/admin/ads/index');

	}
	
	function admin_editZone()
	{
		$this->pageTitle = __("Edit Zone");
		//Zone is a configuration model.
		$this->Zone->cache = true;
		if(!empty($this->data))
		{
			if($this->data['Zone']['zone'])
			{
				$zoneOptions = explode("\n",$this->data['Zone']['zone']);
				$this->data['Zone']['zone'] = serialize($zoneOptions);
			}
			$this->Zone->create();
			if($this->Zone->save($this->data,array('validate'=>'first')))
			{
				//delete ad for zone that does not exists.
				$this->Ad->deleteAfterZoneEdit();
				$this->successMsg(__('Zone list has been updated successfully.'));
				$this->redirect('/admin/ads');
			}else
			{
				$this->errorMsg(__('Sorry, zone list could not be updated.'));
			}
		}else
		{
			$this->data['Zone']['zone'] = implode("\n",$this->Ad->_getZoneOptionList());
		}
	}
}
?>